package com.zf.shuashua.ui.mine;


import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.aliyun.svideo.common.utils.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.vise.xsnow.http.ViseHttp;
import com.zf.shuashua.BuildConfig;
import com.zf.shuashua.R;
import com.zf.shuashua.api.Constants;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.base.BaseActivity1;
import com.zf.shuashua.bean.Me;
import com.zf.shuashua.manager.track.TrackConstants;
import com.zf.shuashua.manager.track.TrackManager;
import com.zf.shuashua.ui.gift.H5Activity;
import com.zf.shuashua.utils.NotificationUtil;
import com.zf.shuashua.utils.ToNotification;

import java.net.Proxy;

public class SettingActivity extends BaseActivity1 implements View.OnClickListener {
    private Toolbar mToolbar;
    private TextView mTvTitle;
    private TextView tvCache;
    private TextView tvUid;
    private TextView tvVersion;
    private ImageView cbPush;
    private Me.DataBean data;
    private View llKill;
    private LinearLayout llVersion;

    @Override
    protected void initView() {
        data = (Me.DataBean) getIntent().getSerializableExtra(com.zf.shuashua.ui.mine.MineFragment.EditPersonal);
        mToolbar = findViewById(R.id.toolbar);
        mTvTitle = findViewById(R.id.tv_title);
        initToolBar(mToolbar, true, "");
        tvUid = findViewById(R.id.tvUid);
        tvVersion = findViewById(R.id.tvVersion);
        mTvTitle.setText("设置");
        tvCache = findViewById(R.id.tvCache);
        cbPush = findViewById(R.id.cb_push);
        llVersion = findViewById(R.id.ll_version);

        tvVersion.setText(BuildConfig.ENABLE_DEBUG ? "DEBUG版本号：" + getVersionName() : "版本号：" + getVersionName());
        llVersion.setOnLongClickListener(v -> {
            Store.getInstance().setProxy(true);
            return true;
        });

        tvUid.setText("我的UID：" + data.getLogin_user_id());
        findViewById(R.id.llHelp).setOnClickListener(this);
        findViewById(R.id.lltoProtocol).setOnClickListener(this);
        findViewById(R.id.lltoPolicy).setOnClickListener(this);
        findViewById(R.id.llAbout).setOnClickListener(this);
        findViewById(R.id.llBalack).setOnClickListener(this);
        findViewById(R.id.llAccount).setOnClickListener(this);
        findViewById(R.id.llCommonProblem).setOnClickListener(this);
        findViewById(R.id.llProblem).setOnClickListener(this);
        findViewById(R.id.llProblem).setOnClickListener(this);
        findViewById(R.id.lltoBusiness).setOnClickListener(this);
        findViewById(R.id.lltoSpecial).setOnClickListener(this);
        llKill = findViewById(R.id.llKill);
        llKill.setOnClickListener(this);
        cbPush.setOnClickListener(this);
        if (!Store.getInstance().isLogin()) {
            llKill.setVisibility(View.GONE);
        }
        ViseHttp.CONFIG().proxy(Proxy.NO_PROXY);
        Store.getInstance().setProxy(false);

        findViewById(R.id.llCache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO
                ToastUtils.show(SettingActivity.this, "已成功清理0MB");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        cbPush.setSelected(NotificationUtil.isNotifyEnabled(this));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    private boolean ToLogin() {
        if (!Store.getInstance().isLogin()) {
            startActivity(new Intent(SettingActivity.this, LoginActivity.class));
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llHelp:
                if (!ToLogin()) {
                    startActivity(new Intent(this, H5Activity.class).putExtra("money", Constants.H5_URL + "helpCenter?version=" + getVersionName()).putExtra("title", "帮助中心"));
                }
                break;
            case R.id.lltoProtocol:
                startActivity(new Intent(this, H5Activity.class).putExtra("money", Constants.H5_URL + "protocol").putExtra("title", "用户协议"));
                break;
            case R.id.lltoPolicy:
                startActivity(new Intent(this, H5Activity.class).putExtra("money", Constants.H5_URL + "policy").putExtra("title", "隐私政策"));
                break;
            case R.id.llAbout:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.cb_push:
                ToNotification.open(this);
                break;
            case R.id.llKill:
                new XPopup.Builder(SettingActivity.this)
                        .dismissOnTouchOutside(false)
                        .asConfirm("提示", "确认退出?", "取消", "确定",
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        //移除别名ID
                                        PushAgent.getInstance(SettingActivity.this).deleteAlias(Store.getInstance().getuserId(), "ALIAS_TYPE_LOGIN", new UTrack.ICallBack() {
                                            @Override
                                            public void onMessage(boolean isSuccess, String message) {
                                            }
                                        });

                                        Store.getInstance().logout(SettingActivity.this);
                                        finish();
                                    }
                                }, null, false)
                        .show();
                break;
            //黑名单
            case R.id.llBalack:
                if (!ToLogin()) {
                    startActivity(new Intent(this, BlackListActivity.class));
                }
                break;
            case R.id.llAccount:
                if (!ToLogin()) {
                    startActivity(new Intent(this, AccountManagement.class));
                }
                break;
            case R.id.llCommonProblem:
                TrackManager.getInstance().reportAdvertAction(TrackConstants.PAGE_ID_NEW, TrackConstants.ACTION_MY_FAQ, TrackConstants.NOTE_MY_FAQ);
                startActivity(new Intent(this, H5Activity.class).putExtra("money", Constants.H5_URL + "qaList").putExtra("title", "常见问题"));
                break;
            case R.id.llProblem:
                TrackManager.getInstance().reportAdvertAction(TrackConstants.PAGE_ID_NEW, TrackConstants.ACTION_MY_PROBLEM, TrackConstants.NOTE_MY_PROBLEM);
                if (!ToLogin()) {
                    startActivity(new Intent(this, H5Activity.class).putExtra("money", Constants.H5_URL + "feedback").putExtra("title", "问题反馈"));
                }
                break;
            case R.id.lltoBusiness:
                startActivity(new Intent(this, H5Activity.class).putExtra("money", Constants.H5_URL + "cooperation").putExtra("title", "商务合作"));
                break;
            case R.id.lltoSpecial:
                startActivity(new Intent(this, H5Activity.class).putExtra("money", Constants.H5_URL + "thanks").putExtra("title", "特别鸣谢"));
                break;
        }
    }
}
